package com.jhd.fuzhou.view.special;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.google.gson.Gson;
import com.jhd.fuzhou.util.AnswerUtil;
import com.jhd.fuzhou.view.daily.DailyStepThreeActivity;
import com.jhd.fuzhou.view.login.BaseActivity;
import com.jhd.fuzhou.view.login.FuzhouApplication;
import com.jhd.fuzhou.R;
import com.jhd.fuzhou.service.SupervisionService;
import com.jhd.fuzhou.util.CustomToast;
import com.jhd.fuzhou.util.TextDialog;
import com.jhd.fuzhou.util.UtilHelper;
import com.jhd.fuzhou.vo.DetectedItemsVo;
import com.jhd.fuzhou.vo.IllegalRecordDtlVo;
import com.jhd.fuzhou.vo.JsonForResult;
import com.jhd.fuzhou.vo.QuestionBankOptionVo;
import com.jhd.fuzhou.vo.SupervisionInspectionContentVo;

import net.zigin.commons.util.ParamsMapUtil;
import net.zigin.commons.util.StringUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

import static com.jhd.fuzhou.view.login.MainActivity.supervisionInspectionVo;


public class SpecialStepThreeActivity extends BaseActivity {
    private Context context = this;
    @BindView(R.id.rv_question_tree)
    RecyclerView rv_question_tree;
    @BindView(R.id.tv_specialityDetail)
    TextView tv_specialityDetail;
    @BindView(R.id.tv_illegal_num)
    TextView tv_illegal_num;
    @BindView(R.id.btn_back)
    Button btn_back;
    @BindView(R.id.btn_next)
    Button btn_next;
    @BindView(R.id.rb_status_1)
    RadioButton rb_status_1;
    @BindView(R.id.rb_status_2)
    RadioButton rb_status_2;
    @BindView(R.id.rb_status_3)
    RadioButton rb_status_3;
    @BindView(R.id.rb_status_4)
    RadioButton rb_status_4;
    @BindView(R.id.rel_quick_check)
    RelativeLayout rel_quick_check;
    @BindView(R.id.rel_laboratory_check)
    RelativeLayout rel_laboratory_check;
    @BindView(R.id.lin_illegal)
    LinearLayout lin_illegal;
    @BindView(R.id.et_supervision)
    EditText et_supervision;
    @BindView(R.id.et_check_record)
    EditText et_check_record;
    @BindView(R.id.tv_laboratory_check)
    TextView tv_laboratory_check;
    @BindView(R.id.tv_quick_check)
    TextView tv_quick_check;
    @BindView(R.id.tv_supervision)
    TextView tv_supervision;
    private AlertDialog _alertDialog = null;
    private List<DetectedItemsVo> detectedItemsVoList = new ArrayList<>();//所有的检测
    private List<DetectedItemsVo> detectedItemsVoList1 = new ArrayList<>();//快速检测
    private List<DetectedItemsVo> detectedItemsVoList2 = new ArrayList<>();//实验室检测
    private List<SupervisionInspectionContentVo> supervisionInspectionContentList = null;
    JSONArray jsonArray = new JSONArray();
    private QuestionTreeAdapter adapter;

    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.activity_step_three);
    }

    @Override
    protected void processLogic() {
        application.addStepActivity(this);
        jsonArray.add("检测项目");
        getCheckValue(jsonArray);
        getQuestion();
        findViewById(R.id.iv_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });
        tv_specialityDetail.setText(supervisionInspectionVo.getSpecialityDetail());
        et_supervision.setText((StringUtil.isNullOrEmpty(supervisionInspectionVo.getSupervisionOpinions()) ? "" : supervisionInspectionVo.getSupervisionOpinions()));
        et_check_record.setText((StringUtil.isNullOrEmpty(supervisionInspectionVo.getInspectRecords()) ? "" : supervisionInspectionVo.getInspectRecords()));
    }

    @OnClick({R.id.btn_next, R.id.btn_back, R.id.rel_quick_check, R.id.rel_laboratory_check, R.id.lin_illegal, R.id.rb_status_3, R.id.rb_status_4, R.id.et_supervision, R.id.et_check_record})
    public void onClick(View v) {
        Gson gson = new Gson();
        switch (v.getId()) {
            case R.id.btn_next:
                next();
                break;
            case R.id.et_supervision:
                TextDialog.showDialog(context, "监督意见", et_supervision);
                break;
            case R.id.et_check_record:
                TextDialog.showDialog(context, "检查记录", et_check_record);
                break;

            case R.id.btn_back:
                onBackPressed();
                break;
            case R.id.rel_quick_check:
                if (checkValue == null) {
                    getCheckValue(jsonArray);
                } else {
                    Intent intent1 = new Intent(context, SpecialQuickCheckActivity.class);
                    intent1.putExtra("checkValue", gson.toJson(checkValue.get(0)));
                    startActivity(intent1);
                }
                break;
            case R.id.rel_laboratory_check:
                if (checkValue == null) {
                    getCheckValue(jsonArray);
                } else {
                    Intent intent2 = new Intent(context, SpecialLaboratoryCheckActivity.class);
                    intent2.putExtra("checkValue", gson.toJson(checkValue.get(0)));
                    startActivity(intent2);
                }
                break;
            case R.id.rb_status_3://处罚1
                supervisionInspectionVo.setPunish("1");
                break;
            case R.id.rb_status_4://不处罚0
                supervisionInspectionVo.setPunish("0");
                break;
            case R.id.lin_illegal:
                startActivity(new Intent(context, SpecialIllegalBehaviorActivity.class));
                break;

        }
    }

    public void next() {
        boolean isAll = true;
        for (SupervisionInspectionContentVo supervisionInspectionContentVo : supervisionInspectionContentList) {
            if (StringUtil.isNullOrEmpty(supervisionInspectionContentVo.getQuestionBankOptionId())) {
                if (supervisionInspectionContentVo.getQuestionBankOptionVoList() != null) {
                    CustomToast.showText(context, "还有未选择的问题!", 3000, 0, 0).show();
                    isAll = false;
                    break;
                }
            }
        }
        if (!rb_status_3.isChecked() && !rb_status_4.isChecked()) {
            CustomToast.showText(context, "还未选择是否处罚!", 3000, 0, 0).show();
        } else {
            if (isAll) {
                String checkRecord = et_check_record.getText().toString();
                String supervision = et_supervision.getText().toString();
                if (!StringUtil.isNullOrEmpty(checkRecord)) {
                    supervisionInspectionVo.setInspectRecords(checkRecord);
                } else {
                    supervisionInspectionVo.setInspectRecords(null);
                }
                if (!StringUtil.isNullOrEmpty(supervision)) {
                    supervisionInspectionVo.setSupervisionOpinions(supervision);
                } else {
                    supervisionInspectionVo.setSupervisionOpinions(null);
                }
                supervisionInspectionVo.setSpecialSuperviseInspectionContentVoList(supervisionInspectionContentList);
                Intent intent = new Intent(context, SpecialStepFourActivity.class);
                startActivity(intent);
            }
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        upUi();
    }

    private void upUi() {
        if (!StringUtil.isNullOrEmpty(supervisionInspectionVo.getPunish())) {
            if ("0".equals(supervisionInspectionVo.getPunish())) {
                rb_status_4.setChecked(true);
            } else if ("1".equals(supervisionInspectionVo.getPunish())) {
                rb_status_3.setChecked(true);
            }
        }
        if (isQualified()) {
            rb_status_1.setChecked(true);
            supervisionInspectionVo.setInspectResult("1");
        } else {
            rb_status_2.setChecked(true);
            supervisionInspectionVo.setInspectResult("0");
        }
        if (supervisionInspectionVo.getIllegalRecordDtlVoList() != null) {
            List<IllegalRecordDtlVo> illegalRecordDtlVoList = supervisionInspectionVo.getIllegalRecordDtlVoList();
            tv_illegal_num.setText(String.valueOf(supervisionInspectionVo.getIllegalRecordDtlVoList().size()));
            if (illegalRecordDtlVoList.size() > 0) {
                if("公共场所卫生监督科".equals(application.getDeptName())){
                    tv_supervision.setVisibility(View.VISIBLE);
                }
                String basis = "";
                for (IllegalRecordDtlVo illegalRecordDtlVo : illegalRecordDtlVoList) {
                    if (basis.equals("")) {
                        basis = illegalRecordDtlVo.getSuperviseOpinion();
                    } else {
                        basis = basis + "\n" + illegalRecordDtlVo.getSuperviseOpinion();
                    }
                }
                tv_supervision.setText(basis);
            } else {
                tv_supervision.setVisibility(View.GONE);
            }
        } else {
            if (!StringUtil.isNullOrEmpty(supervisionInspectionVo.getId())) {
                getIllegalRecordDtlById();
            } else {
                tv_supervision.setVisibility(View.GONE);
            }
        }
        if (supervisionInspectionVo.getIllegalRecordDtlVoList() != null) {
            tv_illegal_num.setText(String.valueOf(supervisionInspectionVo.getIllegalRecordDtlVoList().size()));
        }
        if (supervisionInspectionVo.getDetectedItemsVoList() != null) {
            detectedItemsVoList1.clear();
            detectedItemsVoList2.clear();
            detectedItemsVoList = supervisionInspectionVo.getDetectedItemsVoList();
            for (DetectedItemsVo detectedItemsVo : detectedItemsVoList) {
                if (detectedItemsVo.getCheckType().equals("0")) {
                    detectedItemsVoList1.add(detectedItemsVo);
                } else if (detectedItemsVo.getCheckType().equals("1")) {
                    detectedItemsVoList2.add(detectedItemsVo);
                }
            }
            if (detectedItemsVoList1.size() > 0) {
                tv_quick_check.setTextColor(getResources().getColor(R.color.black));
                tv_quick_check.setText("已检测");
            } else {
                tv_quick_check.setTextColor(getResources().getColor(R.color.search));
                tv_quick_check.setText("未检测");
            }
            if (detectedItemsVoList2.size() > 0) {
                tv_laboratory_check.setTextColor(getResources().getColor(R.color.black));
                tv_laboratory_check.setText("已检测");
            } else {
                tv_laboratory_check.setTextColor(getResources().getColor(R.color.search));
                tv_laboratory_check.setText("未检测");
            }
        }
    }

    private void getIllegalRecordDtlById() {
        Retrofit retrofit = UtilHelper.retrofit(application, UtilHelper.baseUrls.API());//URL的头
        SupervisionService vs = retrofit.create(SupervisionService.class);
        Map<String, String> params = ParamsMapUtil.getInstance("id", supervisionInspectionVo.getId());
        Call<JsonForResult> jsonForResultCall = vs.getIllegalRecordDtlById(application.getAccessToken(), params);
        jsonForResultCall.enqueue(new Callback<JsonForResult>() {
            @Override
            public void onResponse(Call<JsonForResult> call, Response<JsonForResult> response) {
                try {
                    JsonForResult jsonForResult = response.body();
                    if (jsonForResult != null && jsonForResult.isSuccess()) {
                        //请求授权信息
                        if (jsonForResult.getData() != null) {
                            List<IllegalRecordDtlVo> illegalRecordDtlVoList = JSON.parseArray(jsonForResult.getData().toString(), IllegalRecordDtlVo.class);
                            supervisionInspectionVo.setIllegalRecordDtlVoList(illegalRecordDtlVoList);
                            if (illegalRecordDtlVoList != null && illegalRecordDtlVoList.size() > 0) {
                                if("公共场所卫生监督科".equals(application.getDeptName())){
                                    tv_supervision.setVisibility(View.VISIBLE);
                                }
                                String basis = "";
                                for (IllegalRecordDtlVo illegalRecordDtlVo : illegalRecordDtlVoList) {
                                    if (basis.equals("")) {
                                        basis = illegalRecordDtlVo.getSuperviseOpinion();
                                    } else {
                                        basis = basis + "\n" + illegalRecordDtlVo.getSuperviseOpinion();
                                    }
                                }
                                tv_supervision.setText(basis);
                            } else {
                                tv_supervision.setVisibility(View.GONE);
                            }
                        }
                    }
                } catch (Exception e) {
                    UtilHelper.alertMsg(context, "错误", e.getMessage());
                }
            }

            @Override
            public void onFailure(Call<JsonForResult> call, Throwable t) {
                String msg = "获取违法记录失败!";
                UtilHelper.alertMsg(context, "错误", msg);
            }
        });
    }

    private void getQuestion() {
        supervisionInspectionContentList = supervisionInspectionVo.getSpecialSuperviseInspectionContentVoList();
        if (supervisionInspectionContentList != null) {
            LinearLayoutManager layoutManager = new LinearLayoutManager(context);
            rv_question_tree.setLayoutManager(layoutManager);
            adapter = new QuestionTreeAdapter(supervisionInspectionContentList);
            rv_question_tree.setAdapter(adapter);
        }
    }
    public void changeStatus(Context context,int titleLevel, List<SupervisionInspectionContentVo> supervisionInspectionContentList, SupervisionInspectionContentVo supervisionInspectionContentVo) {
        LayoutInflater mLayoutInflater = LayoutInflater.from(context);
        View view = mLayoutInflater.inflate(R.layout.dialog_change_choose, null);
        final androidx.appcompat.app.AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context, R.style.ActionSheetDialogStyle);
        alertBuilder.setView(view);
        AlertDialog _alertDialog = alertBuilder.create();
        //获取当前Activity所在的窗体
        Window window = _alertDialog.getWindow();
        window.setGravity(Gravity.CENTER);
        _alertDialog.show();
        ImageView iv_dismiss = (ImageView) view.findViewById(R.id.iv_dismiss);
        RecyclerView rv_choose = (RecyclerView) view.findViewById(R.id.rv_choose);
        LinearLayoutManager layoutManage = new LinearLayoutManager(context);
        rv_choose.setLayoutManager(layoutManage);
        List<QuestionBankOptionVo> questionBankOptionVoList=new ArrayList<>();
        for (SupervisionInspectionContentVo inspectionContentVo : supervisionInspectionContentList) {
            if(inspectionContentVo.getLevel()==titleLevel+1){
                questionBankOptionVoList=inspectionContentVo.getQuestionBankOptionVoList();
            }
        }
        QuestionBankOptionVoAdapter recyclerAdapter=new QuestionBankOptionVoAdapter(_alertDialog,titleLevel,questionBankOptionVoList,supervisionInspectionContentVo);
        rv_choose.setAdapter(recyclerAdapter);
        iv_dismiss.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                _alertDialog.dismiss();
            }
        });
        WindowManager.LayoutParams params = window.getAttributes() ;
        params.width =(int) (((Activity)context).getWindowManager().getDefaultDisplay().getWidth());                     //使用这种方式更改了dialog的框宽
        window.setAttributes(params);
    }

    public class QuestionBankOptionVoAdapter extends RecyclerView.Adapter<QuestionBankOptionVoAdapter.ViewHolder> {
        private List<QuestionBankOptionVo> questionBankOptionVoList;
        private SupervisionInspectionContentVo supervisionInspectionContentVo;AlertDialog _alertDialog;int titleLevel;
        public QuestionBankOptionVoAdapter( AlertDialog _alertDialog,int titleLevel,List<QuestionBankOptionVo> questionBankOptionVoList,SupervisionInspectionContentVo supervisionInspectionContentVo) {
            this.questionBankOptionVoList = questionBankOptionVoList;
            this.supervisionInspectionContentVo = supervisionInspectionContentVo;
            this._alertDialog = _alertDialog;
            this.titleLevel = titleLevel;
        }
        @NonNull
        @Override
        public QuestionBankOptionVoAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_question_choose, parent, false);
            QuestionBankOptionVoAdapter.ViewHolder viewHolder = new QuestionBankOptionVoAdapter.ViewHolder(itemView);
            return viewHolder;
        }
        @Override
        public void onBindViewHolder(@NonNull QuestionBankOptionVoAdapter.ViewHolder holder, int position) {
            holder.tv_choose.setText(questionBankOptionVoList.get(position).getOptionName());
            holder.relativeLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    for (SupervisionInspectionContentVo inspectionContentVo : supervisionInspectionContentList) {
                        if(inspectionContentVo.getLevel()==titleLevel+1){
                            for (QuestionBankOptionVo questionBankOptionVo : inspectionContentVo.getQuestionBankOptionVoList()) {
                                if(holder.tv_choose.getText().toString().equals(questionBankOptionVo.getOptionName())){
                                    inspectionContentVo.setQuestionBankOptionId(questionBankOptionVo.getId());
                                    inspectionContentVo.setBankOptionId(questionBankOptionVo.getId());//选项id
                                }
                            }
                        }
                    }
                    adapter.notifyDataSetChanged();
                    _alertDialog.dismiss();
                }
            });
        }
        @Override
        public int getItemCount() {
            return questionBankOptionVoList.size();
        }
        class ViewHolder extends RecyclerView.ViewHolder {
            TextView tv_choose;
            ImageView image;
            RelativeLayout relativeLayout;
            public ViewHolder(View itemView) {
                super(itemView);
                this.tv_choose = itemView.findViewById(R.id.tv_choose);
                this.image = itemView.findViewById(R.id.image);
                this.relativeLayout = itemView.findViewById(R.id.relativeLayout);
            }
        }
    }
    public class QuestionTreeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
        private List<SupervisionInspectionContentVo> supervisionInspectionContentList;

        public QuestionTreeAdapter(List<SupervisionInspectionContentVo> supervisionInspectionContentList) {
            this.supervisionInspectionContentList = supervisionInspectionContentList;
        }

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            RecyclerView.ViewHolder viewHolder = getViewHolderByViewType(viewType);
            return viewHolder;
        }

        private RecyclerView.ViewHolder getViewHolderByViewType(int viewType) {
            View itemTitle = View.inflate(context, R.layout.item_question, null);
            View itemTitle2 = View.inflate(context,R.layout.item_question3, null);
            View itemView = View.inflate(context, R.layout.item_question_tree_2, null);
            RecyclerView.ViewHolder holder = null;
            switch (viewType) {
                case 0:
                    holder = new QuestionTreeAdapter.ViewHolderTitle(itemTitle);
                    break;
                case 1:
                    holder = new QuestionTreeAdapter.ViewHolderItem(itemView);
                    break;
                case 2:
                    holder=new QuestionTreeAdapter.ViewHolderTitle2(itemTitle2);
                    break;
            }
            return holder;
        }

        @Override
        public int getItemViewType(int position) {
            if (supervisionInspectionContentList.get(position).getQuestionBankOptionVoList() == null) {
                return 0;
            } else {
                return 1;
            }
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
            if (getItemViewType(position) == 0) {
                ((QuestionTreeAdapter.ViewHolderTitle) holder).tv_question.setText(supervisionInspectionContentList.get(position).getQuestionName());
            }
            if (getItemViewType(position) == 1) {
                SupervisionInspectionContentVo supervisionInspectionContentVo=supervisionInspectionContentList.get(position);
                ((QuestionTreeAdapter.ViewHolderItem) holder).tv_question.setText(supervisionInspectionContentVo.getQuestionName());

                if (!StringUtil.isNullOrEmpty(supervisionInspectionContentVo.getBankOption())) {
                    if(!StringUtil.isNullOrEmpty(supervisionInspectionContentVo.getDefaultValue())){
                        supervisionInspectionContentVo.setBankOption(supervisionInspectionContentVo.getDefaultValue());
                    }
                }
                if(!StringUtil.isNullOrEmpty(supervisionInspectionContentVo.getBankOption())){
                    ((QuestionTreeAdapter.ViewHolderItem)holder).tv_answer.setText(AnswerUtil.setAnswer(supervisionInspectionContentVo.getBankOption()));
                    ((QuestionTreeAdapter.ViewHolderItem)holder).tv_answer.setTextColor(getResources().getColor(R.color.header));
                }
                if (isQualified()) {
                    rb_status_1.setChecked(true);
                    supervisionInspectionVo.setInspectResult("1");
                } else {
                    rb_status_2.setChecked(true);
                    supervisionInspectionVo.setInspectResult("0");
                }
                ((QuestionTreeAdapter.ViewHolderItem) holder).linearLayout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        showChoose(((QuestionTreeAdapter.ViewHolderItem) holder).tv_answer,supervisionInspectionContentVo.getEnforceBasisId(),supervisionInspectionContentVo);
                    }
                });
            }
        }

        @Override
        public int getItemCount() {
            return supervisionInspectionContentList.size();
        }

        class ViewHolderTitle extends RecyclerView.ViewHolder {
            TextView tv_question;

            public ViewHolderTitle(View itemView) {
                super(itemView);
                tv_question = (TextView) itemView.findViewById(R.id.tv_question);
            }
        }
        class ViewHolderTitle2 extends RecyclerView.ViewHolder {
            TextView tv_question;TextView tv_change_status;
            public ViewHolderTitle2(View itemView) {
                super(itemView);
                tv_question = (TextView) itemView.findViewById(R.id.tv_question);
                tv_change_status = (TextView) itemView.findViewById(R.id.tv_change_status);
            }
        }

        class ViewHolderItem extends RecyclerView.ViewHolder {
            TextView tv_question, tv_answer;
            LinearLayout linearLayout;

            public ViewHolderItem(View itemView) {
                super(itemView);
                tv_question = (TextView) itemView.findViewById(R.id.tv_question);
                tv_answer = (TextView) itemView.findViewById(R.id.tv_answer);
                linearLayout = (LinearLayout) itemView.findViewById(R.id.linearLayout);
            }
        }
    }

    private void showChoose(TextView textView, String id,SupervisionInspectionContentVo supervisionInspectionContentVo) {
        LayoutInflater mLayoutInflater = LayoutInflater.from(this);
        View view = mLayoutInflater.inflate(R.layout.dialog_is_fit, null);
        final AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context, R.style.ActionSheetDialogStyle);
        alertBuilder.setView(view);
        _alertDialog = alertBuilder.create();
        //获取当前Activity所在的窗体
        Window window = _alertDialog.getWindow();
        window.setGravity(Gravity.BOTTOM);
        _alertDialog.show();
        ImageView iv_dismiss = (ImageView) view.findViewById(R.id.iv_dismiss);
        RecyclerView rv_question_answer = (RecyclerView) view.findViewById(R.id.rv_question_answer);
        LinearLayoutManager layoutManage = new LinearLayoutManager(context);
        rv_question_answer.setLayoutManager(layoutManage);
        if(!StringUtil.isNullOrEmpty(supervisionInspectionContentVo.getAllowValue())){
            String[] answers = supervisionInspectionContentVo.getAllowValue().split(","); // 使用逗号作为分隔符进行切割
            QuestionBankOptionAdapter recyclerAdapter = new QuestionBankOptionAdapter(textView, id, _alertDialog, answers,supervisionInspectionContentVo);
            rv_question_answer.setAdapter(recyclerAdapter);
        }
        iv_dismiss.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                _alertDialog.dismiss();
            }
        });
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = (int) (getWindowManager().getDefaultDisplay().getWidth());                     //使用这种方式更改了dialog的框宽
        window.setAttributes(params);

    }

    public class QuestionBankOptionAdapter extends RecyclerView.Adapter<QuestionBankOptionAdapter.ViewHolder> {
        private SupervisionInspectionContentVo supervisionInspectionContentVo;
        private TextView textView;
        private String id;
        private AlertDialog _alertDialog;
        private int checkPosition = -1;
        private String[] answers;

        public QuestionBankOptionAdapter(TextView textView, String id, AlertDialog _alertDialog,String[] answers,SupervisionInspectionContentVo supervisionInspectionContentVo) {
            this.supervisionInspectionContentVo = supervisionInspectionContentVo;
            this._alertDialog = _alertDialog;
            this.textView = textView;
            this.id = id;
            this.answers = answers;
        }

        @Override
        public QuestionBankOptionAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_question_choose, parent, false);
            QuestionBankOptionAdapter.ViewHolder viewHolder = new QuestionBankOptionAdapter.ViewHolder(itemView);
            return viewHolder;
        }

        @Override
        public void onBindViewHolder(@NonNull QuestionBankOptionAdapter.ViewHolder holder, int position) {
            holder.tv_choose.setText(AnswerUtil.setAnswer(supervisionInspectionContentVo.getBankOption()));
            if (AnswerUtil.setAnswer(supervisionInspectionContentVo.getBankOption()).equals(textView.getText().toString())) {
                holder.image.setVisibility(View.VISIBLE);
            } else {
                if (supervisionInspectionContentVo.getBankOption().equals("1") && textView.getText().toString().equals("请选择")) {
                    holder.image.setVisibility(View.VISIBLE);
                } else {
                    holder.image.setVisibility(View.GONE);
                }
            }

            holder.relativeLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    supervisionInspectionContentVo.setBankOption(answers[position]);
                    textView.setText(AnswerUtil.setAnswer(answers[position]));
                    textView.setTextColor(getResources().getColor(R.color.header));
                    holder.image.setVisibility(View.VISIBLE);
                    _alertDialog.dismiss();
                    checkPosition = position;
                    notifyDataSetChanged();
                    if (answers[position].equals("2")) {
                        if (id != null) {
                            getEnforceBasisById(id);
                        } else {
                            startActivity(new Intent(context, SpecialCheckContentActivity.class));
                        }
                    }
                    if (isQualified()) {
                        rb_status_1.setChecked(true);
                        supervisionInspectionVo.setInspectResult("1");
                    } else {
                        rb_status_2.setChecked(true);
                        supervisionInspectionVo.setInspectResult("0");
                    }
                }
            });

            if (position == checkPosition) {
                holder.image.setVisibility(View.VISIBLE);
            } else {
                if (checkPosition != -1) {
                    holder.image.setVisibility(View.GONE);
                }
            }
        }

        @Override
        public int getItemCount() {
            return answers.length;
        }

        class ViewHolder extends RecyclerView.ViewHolder {
            TextView tv_choose;
            ImageView image;
            RelativeLayout relativeLayout;

            public ViewHolder(View itemView) {
                super(itemView);
                this.tv_choose = itemView.findViewById(R.id.tv_choose);
                this.image = itemView.findViewById(R.id.image);
                this.relativeLayout = itemView.findViewById(R.id.relativeLayout);
            }
        }
    }


    public boolean isQualified() {
        for (SupervisionInspectionContentVo supervisionInspectionContentVo : supervisionInspectionContentList) {
            List<QuestionBankOptionVo> questionBankOptionVos = supervisionInspectionContentVo.getQuestionBankOptionVoList();
            if (questionBankOptionVos != null) {
                for (QuestionBankOptionVo questionBankOptionVo : questionBankOptionVos) {
                    if (questionBankOptionVo.getId().equals(supervisionInspectionContentVo.getQuestionBankOptionId()) &&
                            (("否").equals(questionBankOptionVo.getOptionName()) || ("不合格").equals(questionBankOptionVo.getOptionName()))) {
                        return false;
                    }
                }
            }
        }
        return true;
    }

    /*获取详情失败*/
    private void getEnforceBasisById(String id) {
        Retrofit retrofit = UtilHelper.retrofit(application, UtilHelper.baseUrls.API());//URL的头
        SupervisionService vs = retrofit.create(SupervisionService.class);
        Map<String, String> params = ParamsMapUtil.getInstance("id", (id == null) ? "" : id);
        Call<JsonForResult> jsonForResultCall = vs.getEnforceBasisById(application.getAccessToken(), params);//调用接口获取json返回
        jsonForResultCall.enqueue(new Callback<JsonForResult>() {
            @Override
            public void onResponse(Call<JsonForResult> call, Response<JsonForResult> response) {
                try {
                    JsonForResult jsonForResult = response.body();
                    if (jsonForResult != null && jsonForResult.isSuccess()) {
                        if (jsonForResult.getData() != null) {//如果有数据，将数据带过去显示
                            IllegalRecordDtlVo illegalRecordDtlVo = JSON.parseObject(jsonForResult.getData().toString(), IllegalRecordDtlVo.class);
                            Intent intent;
                            if (!StringUtil.isNullOrEmpty(illegalRecordDtlVo.getHandleBasis()) && !StringUtil.isNullOrEmpty(illegalRecordDtlVo.getSuperviseOpinion()) &&
                                    !StringUtil.isNullOrEmpty(illegalRecordDtlVo.getIllegalBehavior()) && !StringUtil.isNullOrEmpty(illegalRecordDtlVo.getQualitativeBasis())) {
                                intent = new Intent(context, SpecialStandardWordActivity.class);
                                Gson gson = new Gson();
                                intent.putExtra("enforceBasis", gson.toJson(jsonForResult.getData()));
                            } else {//是标题项的
                                intent = new Intent(context, SpecialCheckContentActivity.class);
                                intent.putExtra("code", illegalRecordDtlVo.getCode());
                            }
                            context.startActivity(intent);
                        } else {//如果没数据，就到页面全盘选择
                            Intent intent = new Intent(context, SpecialCheckContentActivity.class);
                            context.startActivity(intent);
                        }
                    }
                } catch (Exception e) {
                    UtilHelper.alertMsg(context, "错误", e.getMessage());
                }
            }

            @Override
            public void onFailure(Call<JsonForResult> call, Throwable t) {
                String msg = "网络错误!";
                UtilHelper.alertMsg(context, "错误", msg);
            }
        });
    }
}